<template>
  <div class="SecuritySet">
    <el-form :model="formModel">
      <el-form-item label="系统界面显示水印" prop="watermark">
        <el-switch
          v-model="formModel.watermark"
          @click.native="changeSwitch('watermark', '系统界面显示水印')"
        ></el-switch>
        <div class="tips">
          开启后，所有系统界面的背景均会显示当前登录用户的姓名+账号，以防截屏泄密
        </div>
      </el-form-item>
      <el-form-item label="人员组件属性配置" prop="show_person_setting">
        <el-select
          v-model="formModel.show_person_setting"
          @change="changeSelect"
        >
          <el-option
            v-for="item in personSetOptions"
            :label="item.label"
            :value="item.value"
            :key="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="个人中心所在部门隐藏" prop="dept_hide">
        <el-switch
          v-model="formModel.dept_hide"
          @click.native="changeSwitch('dept_hide', '个人中心所在部门隐藏')"
        ></el-switch>
        <div class="tips">
          开启后，用户在个人中心无法查看自己所在的业务组织部门
        </div>
      </el-form-item>
      <el-form-item label="登录验证码" prop="login_code">
        <el-switch
          v-model="formModel.login_code"
          @click.native="changeSwitch('login_code', '登录验证码')"
        ></el-switch>
        <div class="tips">
          开启后，系统登录界面增加图形验证码区域，增加系统安全系数
        </div>
      </el-form-item>
      <el-form-item label="主部门标识隐藏" prop="main_dept_hide">
        <el-switch
          v-model="formModel.main_dept_hide"
          @click.native="changeSwitch('main_dept_hide', '主部门标识隐藏')"
        ></el-switch>
        <div class="tips">
          开启后，主部门不展示（主）标识，其他部门展示（兼）标识，只影响标识不影响部门显隐
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getAllSettingsMap, setSetting } from "@/http/api";
import { AddWaterMark } from "watermarkerjs";
export default {
  name: "SecuritySet",
  data() {
    return {
      formModel: {
        watermark: false,
        show_person_setting: "phoneNumber",
        dept_hide: false,
        login_code: false,
        main_dept_hide: false,
      },
      personSetOptions: [
        {
          label: "邮箱",
          value: "email",
        },
        {
          label: "账号",
          value: "employee_id",
        },
        {
          label: "手机号",
          value: "phone_number",
        },
        {
          label: "工号",
          value: "work_id",
        },
      ],
    };
  },
  async mounted() {
    let res = await getAllSettingsMap();
    if (res.code === 20041) {
      this.formModel = res.data;
      for (const key in this.formModel) {
        this.formModel[key] === 1
          ? (this.formModel[key] = true)
          : this.formModel[key] === 0
          ? (this.formModel[key] = false)
          : undefined;
      }
    }
  },
  methods: {
    changeSwitch(type, describe) {
      this.$confirm(
        `是否${this.formModel[type] ? "开启" : "关闭"}${describe}`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          callback: async (action) => {
            if (action !== "confirm") {
              this.formModel[type] = !this.formModel[type];
            } else {
              // 发请求改变对应信息
              let res = await setSetting({
                type,
                value: this.formModel[type] === true ? 1 : 0,
              });
              if (res.code === 20031) {
                this.$message.success("修改成功");
              }
            }
          },
        }
      );
    },
    async changeSelect(current) {
      let res = await setSetting({
        type: "show_person_setting",
        value: current,
      });
      if (res.code === 20031) {
        this.$message.success("修改成功");
      }
    },
    async setWaterMark() {
      const watermask = document.querySelector("#watermask");
      if (watermask) {
        if (!this.formModel.watermark) {
          document.body.className = "hiddenMask";
        } else {
          document.body.className = "";
        }
      } else {
        if (this.formModel.watermark) {
          AddWaterMark(JSON.parse(localStorage.getItem("userInfo")));
        }
      }
    },
  },
  watch: {
    "formModel.watermark": function (newVal) {
      this.setWaterMark();
    },
  },
};
</script>
<style lang="less" scoped>
.SecuritySet {
  padding: 0 10px;
  :deep {
    .el-form-item {
      margin-bottom: 30px;
      .el-form-item__label {
        font-weight: 600;
      }
      .tips {
        line-height: 20px;
        color: #8f959e;
        font-size: 14px;
      }
    }
  }
}
</style>